import urllib
import urllib2
from xml.dom import minidom
appid = 'YahooDemo'
def questionSearch(params):
    """Answers questionSearch wrapper"""
    baseUrl = 'http://answers.yahooapis.com/AnswersService/V1/questionSearch?appid='+appid+'&'
    finalUrl = baseUrl
    for k, v in params.items():
        finalUrl = finalUrl + urllib.quote(k) +'=' + urllib.quote(v) + '&'
    finalUrl = finalUrl[:len(finalUrl) - 1]
    print finalUrl
    site = urllib2.urlopen(finalUrl)
    #theXml = site.read()
    #print theXml
    xmlDoc = minidom.parse(site)
    questions = xmlDoc.getElementsByTagName('Question')
    """print questions[0].getElementsByTagName('Date')[0].toxml()
    print getData(questions[0].getElementsByTagName('Date')[0].toxml())"""
    for ques in questions:
        print parseQuestion(ques)['UserId']

def parseQuestion(ques):            
    ques_ = {}
    for node in ques.childNodes:
        if node.nodeType == node.ELEMENT_NODE:
            ques_.update(getData(node.toxml()))
    return ques_
def getData(node):
    i = node.find('>')
    j = node.rfind('<')
    return {node[1:i]:node[i+1:j]}
if __name__ == '__main__':
    questionSearch({'query':'cats', 'type':'open', 'region':'us'})